package com.itszt.manager.service.impl;

import com.alibaba.dubbo.common.json.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itszt.common.*;
import com.itszt.manager.dao.ItemDao;
import com.itszt.manager.service.ItemService;
import com.itszt.pojo.TbItem;
import com.itszt.pojo.TbItemCat;
import com.itszt.pojo.TbItemDesc;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Resource
    private ItemDao itemDao;
    @Value("${nginx_server}")
    private String ngnixUrl;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public EasyUIPageDatasBean<TbItem> showPageItems(String pageNow, String rows) {

        PageHelper.startPage(NumberUtils.createInteger(pageNow), NumberUtils.createInteger(rows));
        List<TbItem> tbItems = itemDao.queryAllItems();

        PageInfo<TbItem> tbItemPageInfo = new PageInfo<>(tbItems);

        int pages = tbItemPageInfo.getPages();
        long total = tbItemPageInfo.getTotal();

        EasyUIPageDatasBean easyUIPageDatasBean = new EasyUIPageDatasBean();
        easyUIPageDatasBean.setRows(tbItems);
        easyUIPageDatasBean.setTotal(total);

        return easyUIPageDatasBean;
    }

    @Override
    public List<EasyUITreeBean> showItemTrees(String parentid) {

        List<TbItemCat> tbItemCats = itemDao.queryCatsByPid(parentid);

        List<EasyUITreeBean> easyUITreeBeans =new ArrayList<>();

        for (TbItemCat tbItemCat : tbItemCats) {
            EasyUITreeBean easyUITreeBean = new EasyUITreeBean();
            easyUITreeBean.setId(tbItemCat.getId());
            easyUITreeBean.setState(tbItemCat.getIsParent()?EasyUITreeBean.STATE_CLOSED:EasyUITreeBean.STATE_OPEN);
            easyUITreeBean.setText(tbItemCat.getName());

            easyUITreeBeans.add(easyUITreeBean);
        }
        return easyUITreeBeans;
    }

    @Override
    public List<EasyUITreeBean> showTopItemCats() {

        return showItemTrees("0");
    }

    @Override
    @Transactional
    public boolean saveItems(TbItem tbItem, TbItemDesc tbItemDesc) throws Exception {
        String imageUrls = tbItem.getImage();
        tbItem.setImage(PicUrlUtil.genGroupPath(imageUrls,ngnixUrl));
        tbItem.setStatus(1);
        tbItem.setCreated(new Date());
        tbItem.setUpdated(new Date());

        itemDao.insertItems(tbItem);

        tbItemDesc.setUpdated(new Date());
        tbItemDesc.setCreated(new Date());
        tbItemDesc.setItemId(tbItem.getId());

        itemDao.insertItemDesc(tbItemDesc);

        MessageItem messageItem = new MessageItem();
        messageItem.setAction(MessageItem.ACTION_ADD);
        messageItem.setTbItem(tbItem);
        kafkaTemplate.send(ConstanKeys.KAFKA_TOPIC_ITEM, JSON.json(messageItem));

        return true;
    }

    @Override
    public TbItem getItemByID(String itemid) {

        TbItem tbItem = itemDao.queryItemByID(itemid);
        tbItem.setImage(PicUrlUtil.genFullPath(tbItem.getImage(),ngnixUrl));

        return tbItem;
    }

    @Override
    public TbItemDesc getItemDescByID(String itemid) {

        return itemDao.queryItemDescByID(itemid);
    }
}
